#include "chargingtcpdrive.h"

bool ChargingTcpDrive::isConnected( QTcpSocket& mSocket ) {
    return mSocket.isValid() && mSocket.isOpen();
}

bool ChargingTcpDrive::setOutput( QTcpSocket& mSocket, ChargingTcpDrive::Operator oper ) {}

bool ChargingTcpDrive::updateInput( QTcpSocket& mSocket ) {}

bool ChargingTcpDrive::updateOutput( QTcpSocket& mSocket ) {}

bool ChargingTcpDrive::open( QTcpSocket& mSocket, const QString& ip, unsigned short port ) {
    if ( isConnected( mSocket ) ) { return true; }

    try {
        mSocket.close();
        mSocket.connectToHost( ip, port );

        // 实际无该相应？
        // if (mSocket.waitForConnected(800)) { return false; }
    }
    catch ( std::runtime_error& e ) {
        qDebug() << e.what();
        return false;
    }

    return isConnected( mSocket );
}

void ChargingTcpDrive::close( QTcpSocket& mSocket ) {
    limits.front = false;
    limits.back = false;
    mSocket.abort();
}
